<?php

/**
 * @filename Backup.php
 * @encoding UTF-8
 *
 * @author 徐小军
 *
 * @link http://www.okd100.com
 *
 * @copyright 南昌欧科达科技有效公司
 * @datetime 2016-8-8  10:37:01
 *
 * @version 1.0
 * @Description  数据库备份操作
 */

namespace app\admin\controller;

//use Ifsnop\Mysqldump as IMysqldump;
use app\admin\model\Backup as BackupModel;
use think\Db;

class Backup extends Common
{
    public function backup()
    {
      if (session('user')->user_type == 'user' or session('user')->user_type == 'agent') {
          return error('没有权限');
      }

        $sql=new \org\Baksql(\think\Config::get("database"));

        try {
            return success( $sql->backup());

        } catch (\Exception $e) {
           return error('error: '.$e->getMessage());
       }



//        $filename = 'backup'.date('ymd-His').'.sql';
//        try {
//            $dump = new IMysqldump\Mysqldump('mysql:host='.config('database.hostname').';dbname='.config('database.database').';port='.config('database.hostport'), config('database.username'), config('database.password'), ['add-drop-table' => true]);
//            $dump->start(ROOT_PATH.'public'.DS.'sql'.DS.$filename);
//            BackupModel::create(['filename' => $filename, 'backup_user' => session('user')->user_name]);
//
//            return success('成功备份数据库');
//        } catch (\Exception $e) {
//            return error('mysqldump-php error: '.$e->getMessage());
//        }
    }
    protected function delete($backup)
    {
        if ($backup) {
            $backup->delete();
        } else {
            throw new Exception('不能删除该备份');
        }
    }
    public function down($backname){
        $sql=new \org\Baksql(\think\Config::get("database"));
        $sql->downloadFile($backname);
    }
    public function del($backname)
    {

        $sql=new \org\Baksql(\think\Config::get("database"));
        $msg=$sql->delfilename($backname);
        return success($msg);

      if (session('user')->user_type != 'superuser') {
          return error('没有权限');
      }
        try {
            $back = BackupModel::get($backup_id);
            $this->delete($back);
        } catch (Exception $e) {
            return error($e->getMessage());
        }

        return success('删除成功');
    }
    public function del_multi($ids)
    {
      if (session('user')->user_type != 'superuser') {
          return error('没有权限');
      }
        $sql=new \org\Baksql(\think\Config::get("database"));
        foreach($ids as $backname){
            $sql->delfilename($backname);
        }
        return success('删除成功');exit;


        $backs = BackupModel::All($ids);
        $sum = 0;
        foreach ($backs as $back) {
            try {
                $this->delete($back);
            } catch (Exception $e) {
                $sum -= 1;
            }
            $sum += 1;
        }

        return success('删除'.$sum.'个备份成功');
    }
    public function restore($backname)
    {
        $sql=new \org\Baksql(\think\Config::get("database"));
       $msg=$sql->restore($backname);
       return success($msg);

        $backup = BackupModel::get($backname);

        if ($backup) {
            if ($backup->check()) {
                //    try {
                    $_sql = file_get_contents($backup->file_path);

                $_arr = explode(';', $_sql);
                Db::batchQuery($_arr);
              //  } catch (\Exception $e) {
              //      return error('恢复失败:'.$e->getMessage());
            //    }
            } else {
                return error('备份已损坏');
            }
        } else {
            return error('不存在这个备份');
        }
    }
    public function showlist()
    {



        $sql=new \org\Baksql(\think\Config::get("database"));

        $list=$sql->get_filelist();


//        $list = BackupModel::order('create_time desc')->paginate(getPageSize());
       $this->assign('list', $list);

        $this->assign('user', session('user'));
        return $this->fetch();
    }
}
